package zhuyun.code.launcheractivity.view

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_profile.*
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.model.NetworkRequest
import zhuyun.code.launcheractivity.model.ResponseDataHandler
import zhuyun.code.launcheractivity.model.data.BaseResponse
import zhuyun.code.launcheractivity.model.data.FriendCircleListBean
import zhuyun.code.launcheractivity.model.data.FriendDetailsBean
import zhuyun.code.launcheractivity.model.data.RecommendFriendsBean
import zhuyun.code.launcheractivity.utils.GlideUtil
import zhuyun.code.launcheractivity.utils.SPUtils
import zhuyun.code.launcheractivity.view.adapter.PeopleYouMayKnowAdapter
import zhuyun.code.launcheractivity.view.adapter.ProfileTimelineAdapter

class ProfileActivity : AppCompatActivity(),View.OnClickListener {
    private lateinit var timelineAdapter:ProfileTimelineAdapter
    private lateinit var commonAdapter:PeopleYouMayKnowAdapter
    private val listTimeline = mutableListOf<FriendCircleListBean>()
    private val listCommon = mutableListOf<RecommendFriendsBean>()
    private var qrUserId:String? = null
    private var mUserId = ""
    private var sourceId = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)
        qrUserId = intent.getStringExtra(Constants.USER_ID)
        mUserId = SPUtils.getUserString(Constants.USER_ID,"")!!
        sourceId = intent.getIntExtra(Constants.SOURCE_ID, 6)
        iv_back.setOnClickListener(this)
        iv_qr_code.setOnClickListener(this)
        bt_add.setOnClickListener(this)

        timelineAdapter = ProfileTimelineAdapter(listTimeline)
        rv_timeline.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        rv_timeline.adapter = timelineAdapter
        rl_timeline.setOnClickListener(this)

        commonAdapter = PeopleYouMayKnowAdapter(listCommon)
        rvCommon.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        rvCommon.adapter = commonAdapter

        getFriendDetails()


    }

    private var isFriend = false

    private fun getFriendDetails(){
        NetworkRequest.getFriendDetails(qrUserId!!, object :ResponseDataHandler<FriendDetailsBean>(){
            override fun onSuccess(
                resultData: FriendDetailsBean?,
                baseResponse: BaseResponse<FriendDetailsBean>
            ) {
                resultData?.let {
                    if(it.FriendCircleList == null){
                        rl_timeline.visibility = View.GONE
                    } else {
                        rl_timeline.visibility = if(it.FriendCircleList.isEmpty()) View.GONE else View.VISIBLE
                    }
                    listTimeline.clear()
                    listTimeline.addAll(resultData.FriendCircleList)
                    timelineAdapter.notifyDataSetChanged()

                    GlideUtil.displayEspImage(it.HeadUrl,iv_avatar,
                        R.drawable.pic_touxiang_normal
                    )
                    tv_name.text = it.Remarks
                    when(it.Sex){
                        0->{
                            iv_sex.setImageResource(R.drawable.icon_female_data)
                        }
                        1->{
                            iv_sex.setImageResource(R.drawable.icon_male_data)
                        }
                    }
                    tv_via.text ="VIA号:${it.UserName}"
                    isFriend = resultData.IsFriend
                    bt_add.visibility = if(isFriend||qrUserId.equals(mUserId)) View.GONE else View.VISIBLE
                    rlCommon.visibility = if(isFriend||qrUserId.equals(mUserId)) View.VISIBLE else View.GONE
                    if(isFriend||qrUserId.equals(mUserId)){
                        getCommonPeople()
                    }


                }



            }

            override fun onFailure(errMsg: String?) {

            }

        })
    }

    private fun getCommonPeople(){
        NetworkRequest.getCommonFriends(qrUserId!!,5,object :ResponseDataHandler<List<RecommendFriendsBean>>(){
            override fun onSuccess(
                resultData: List<RecommendFriendsBean>?,
                baseResponse: BaseResponse<List<RecommendFriendsBean>>
            ) {
                rlCommon.visibility = if(resultData!!.isEmpty()) View.GONE else View.VISIBLE
                listCommon.clear()
                listCommon.addAll(resultData)
                commonAdapter.notifyDataSetChanged()
            }

            override fun onFailure(errMsg: String?) {

            }

        })
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.iv_back->{
                finish()
            }
            R.id.iv_qr_code->{
                startActivity(Intent(this,NewQRCodeActivity::class.java).putExtra(Constants.ID,qrUserId))
            }
            R.id.rl_timeline->{
                startActivity(Intent(this,MyHomePageActivity::class.java)
                    .putExtra(Constants.USER_ID,qrUserId))
            }
            R.id.bt_add->{
                startActivity(Intent(this,FriendVerificationActivity::class.java)
                    .putExtra(Constants.USER_ID,qrUserId)
                    .putExtra("sourceId",sourceId))
            }


        }

    }
}
